<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Menu extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'sys_menus';

		$this->primary_key = 'sys_menus.menu_id';

	}
	
	function get_menu(){
		$this->load->database();					
		$sql = "SELECT * FROM sys_menus WHERE parent='0' ORDER BY `order`;";
		$query = $this->db->query($sql);
		$sys_menus = array();
		foreach($query->result_array() as $row)
		{
			$sys_menus[$row['menu_id']]['data'] = $row;
			$sys_menus[$row['menu_id']]['sub'] = $this->_get_child($row['menu_id']);
		}
		$this->db->close();
		return $sys_menus;
	}
	
	function _get_child($id){
		$this->load->database();					
		$sql = "SELECT * FROM sys_menus WHERE parent='$id' ORDER BY `order`;";
		$query = $this->db->query($sql);		
		$sys_menus = array();
		foreach($query->result_array() as $row)
		{
			$sys_menus[$row['menu_id']]['data'] = $row;
			$sys_menus[$row['menu_id']]['sub'] = $this->_get_child($row['menu_id']);
		}
		$this->db->close();
		return $sys_menus;
	}
	
	function get_list_menu(){
		$this->load->database();					
		$sql = "SELECT menu_id,alias FROM sys_menus ORDER BY menu_id ASC;";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_menu_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM sys_menus WHERE menu_id='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_new_id_access(){
		$this->load->database();					
		$sql = "SELECT MAX(id_access) id FROM sys_menus;";
		$query = $this->db->query($sql);
		$access=100;
		foreach($query->result_array() as $row)
		{
			$access = $row['id'] + 1;
		}
		$this->db->close();
		return $access;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				if($data['parent'] != 0)
					$data['order'] = $data['parent'].'0'.$data['order'];
				$this->db->insert('sys_menus',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
			
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				if($data['parent'] != 0)
					$data['order'] = $data['parent'].'0'.$data['order'];
				$this->db->where('menu_id',$id);
				$this->db->update('sys_menus',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$sql = "DELETE FROM sys_menus WHERE menu_id='$id'";
			$this->db->query($sql);
		$this->db->close();
	}

}

?>